package com.beyond.pm.component.chart

import android.view.ViewGroup
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.viewinterop.AndroidView
import com.beyond.pm.R
import com.beyond.theme.orange500
import com.github.mikephil.charting.charts.RadarChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.RadarData
import com.github.mikephil.charting.data.RadarDataSet
import com.github.mikephil.charting.data.RadarEntry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter

/**
 * @author chen
 * @time 2023/6/12 14:40
 * @name ProjectManage
 * @class describe
 */

@Composable
fun RadarChart(
    modifier: Modifier = Modifier,
    entries: List<RadarEntry>,
    dataSetLabel: String,
    dataSetColor: Color = MaterialTheme.colorScheme.primary,
    animateX: Int = 500,
    animateY: Int = 500
) {
    var chart by remember { mutableStateOf<RadarChart?>(null) }
    val tColor = MaterialTheme.colorScheme.onBackground.toArgb()
    Box(modifier = modifier.fillMaxSize()) {
        Surface(color = MaterialTheme.colorScheme.background) {
            AndroidView(
                factory = { context ->
                    RadarChart(context).apply {
                        layoutParams = ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                        )
                        xAxis.apply {
                            setDrawAxisLine(false) // X坐标轴的线
                            setDrawGridLines(false) //
                            axisLineWidth = 1f
                            setAvoidFirstLastClipping(true)
                            position = XAxis.XAxisPosition.BOTTOM
                            textColor = tColor
                            yOffset = 0f
                            xOffset = 0f
                            axisLineColor = tColor
                            textSize = 12f
                            valueFormatter = object : IndexAxisValueFormatter() {
                                override fun getFormattedValue(value: Float): String {
                                    return "222"
                                }
                            }
                        }
                        yAxis.apply {
                            isEnabled = true
                            setLabelCount(5, true)
                            setDrawLabels(true) // 实现显示线的标题
                            textColor = orange500.toArgb()
//                            axisMaximum = 7f
                            spaceMax = 2f
                            axisMinimum = 0f

                        }
                        setExtraOffsets(5f, 30f, 5f, 30f)
                        data = RadarData(RadarDataSet(entries, dataSetLabel)) // 必须赋值初值
                        description .apply {
                            isEnabled = false
                        }
//                        rotationAngle = 90f
                        legend.apply {
                            isEnabled = false
                        }
                        setNoDataText(context.getString(R.string.no_data))
                        setTouchEnabled(true)
                        setDrawWeb(true)
                        isHighlightPerTapEnabled = false
                        isClickable = false
                        webLineWidth = 1f
                        webLineWidthInner = 1f
                        webAlpha = 100
                        webColor = tColor
                        animateX(animateX)
                        animateY(animateY)
                    }
                },
                update = { view ->
                    chart = view
                    updateChart(chart, entries, dataSetLabel, dataSetColor)
                }
            )
        }
    }
}

private fun updateChart(
    chart: RadarChart?,
    entries: List<RadarEntry>,
    dataSetLabel: String,
    dataSetColor: Color
) {
    chart?.let {
        val dataSet = RadarDataSet(entries, dataSetLabel)
        dataSet.fillColor = dataSetColor.toArgb()
        dataSet.color = dataSetColor.toArgb()
        dataSet.valueTextSize = 8f
        dataSet.valueTextColor = orange500.toArgb()
        dataSet.setDrawFilled(true)
        dataSet.setDrawValues(false)
        val radarData = RadarData(dataSet)

        it.data = radarData
        it.invalidate()
    }
}